package com.firexxx.study.visitor;

import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

import java.util.HashSet;
import java.util.Set;

/**
 * @description: TODO
 * @throws:
 * @author: zhuangzhang2
 * @date: 2020/07/05 01:12
 */
public class TimeClassVisitor extends ClassVisitor {
    private String className = null;
    private static Set<String> skipSet = new HashSet<>();
    private boolean detailSwitch = false;

    static {
//        skipSet.add("");
    }

    public TimeClassVisitor(ClassVisitor classVisitor, String className, boolean detailSwitch) {
        super(Opcodes.ASM7, classVisitor);
        this.className = className.replace('/','.');
        this.detailSwitch = detailSwitch;
    }

    @Override
    public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
        MethodVisitor mv = cv.visitMethod(access, name, descriptor, signature, exceptions);

        //过来待修改类的构造函数
        if (name.equals("<init>") || mv == null) {
            // 对象初始化方法就不增强了
            return mv;
        }

        TimeMethodVisitor timeMethodVisitor = new TimeMethodVisitor(mv, className, name, detailSwitch);
        return timeMethodVisitor;

//        String key = className + ":" + name;
//        return new TimeAdviceAdapter(Opcodes.ASM7, timeMethodVisitor, access, name, descriptor, key);
    }
}